#!/bin/bash

get_script_path() {
  script_file=$BASH_SOURCE
  abs_script_file=$(readlink -f $script_file)
  abs_script_dir=$(dirname $abs_script_file)
}
get_script_path

cd $abs_script_dir

binutils_src_path=`pwd`/tmp/binutils-2.30
install_path=`pwd`/binutils.o

rm -rf tmp

set -e
mkdir -p tmp
cd tmp
if [ ! -e binutils-2.30.tar.xz ]; then
	# wget https://ftp.gnu.org/gnu/binutils/binutils-2.30.tar.xz
  wget https://mirrors.tuna.tsinghua.edu.cn/gnu/binutils/binutils-2.30.tar.xz
fi
tar -xf binutils-2.30.tar.xz

export CFLAGS=-fPIC

cd $binutils_src_path/bfd
./configure --prefix=$install_path --enable-targets=x86_64-unkown-linux --with-arch_32=i686 
make -j4 && make install

cd $binutils_src_path/opcodes
./configure --prefix=$install_path --enable-targets=x86_64-unkown-linux --with-arch_32=i686 
make -j4 && make install
cp disassemble.h $install_path/include

cd $binutils_src_path/libiberty 
./configure --prefix=$install_path --enable-targets=x86_64-unkown-linux --with-arch_32=i686 
make -j4 && make install
mkdir -p $install_path/lib64
cp libiberty.a $install_path/lib64/

cd $binutils_src_path/zlib
./configure --prefix=$install_path --enable-targets=x86_64-unkown-linux --with-arch_32=i686 
make -j4 && make install

# cd $binutils_src_path
# ./configure --prefix=$install_path --enable-targets=x86_64-unkown-linux --with-arch_32=i686 
# make -j5 && make install

cd $abs_script_dir
rm -rf tmp
